package com.sys.basics.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sys.basics.config.annotation.SysLogAnnotation;
import com.sys.basics.entity.SysUser;
import com.sys.basics.entity.vo.RoleResourceVo;
import com.sys.basics.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import com.sys.basics.config.jsonRes.JsonResult;
import com.sys.basics.config.jsonRes.PageParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.sys.basics.entity.SysRole;
import com.sys.basics.service.SysRoleService;
import io.swagger.annotations.*;

import javax.validation.Valid;
import javax.validation.constraints.*;
import java.util.List;

/**
 * @author: ares
 * @date: 2021-10-08
 * @description: 角色表 前端控制器
 */
@Validated
@Slf4j
@Api(tags = "角色管理")
@RestController
@RequestMapping("/basics/sysRole")
public class SysRoleController extends AbstractController<SysRoleService, SysRole> {

    private JsonResult<List<SysRole>> listResult = new JsonResult<>();

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysUserService sysUserService;

    @SysLogAnnotation("查询用户在组织中拥有的角色")
    @ApiOperation("查询用户在组织中拥有的角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query"),
            @ApiImplicitParam(name = "orgId", value = "组织id", required = true, paramType = "query")
    })
    @GetMapping("/getListByUserAndOrg")
    public JsonResult<List<SysRole>> getListByUserAndOrg(@NotNull(message = "用户id不能为空") Long userId, Long orgId) {
        SysUser user = sysUserService.getById(userId);
        if(ObjectUtil.isNull(user)){
            return listResult.error("用户不存在");
        }
        if (orgId == null) {
            orgId = user.getDefaultOrgId();
        }
        return listResult.success("查询用户在组织中拥有的角色", sysRoleService.getListByUserAndOrg(userId,orgId));
    }

    @SysLogAnnotation("根据组织分页查询角色")
    @ApiOperation("根据组织分页查询角色")
    @ApiImplicitParam(name = "orgId", value = "组织id", required = true, paramType = "query")
    @PostMapping("/getPagesByOrg")
    public JsonResult<IPage<SysRole>> getPagesByOrg(@RequestBody PageParam<SysRole> param, @NotNull(message = "组织id不能为空") Long orgId) {
        JsonResult<IPage<SysRole>> returnPage = new JsonResult<IPage<SysRole>>();
        Page<SysRole> page = new Page<SysRole>(param.getPageNum(), param.getPageSize());
        IPage<SysRole> pageData = sysRoleService.getPagesByOrg(page, orgId, param.getParam());
        return returnPage.success(pageData);
    }

    @SysLogAnnotation("新建角色")
    @ApiOperation("新建角色")
    @PostMapping("/createRole")
    public JsonResult createRole(@RequestBody @Valid RoleResourceVo role) {
        SysRole roleByName = sysRoleService.getByOrgAndName(role.getOrgId(), role.getRoleName());
        if (ObjectUtil.isNotNull(roleByName)) {
            return result.error("角色已存在！");
        }
        if (ObjectUtil.isNotNull(role)) {
            int saveNum = sysRoleService.createRoleLinkResource(role);
            if (saveNum>0) {
                result.success("添加角色成功");
            } else {
                result.error("添加角色失败！");
            }
        } else {
            result.error("请传入正确参数！");
        }
        return result;
    }

    @SysLogAnnotation("更新角色")
    @ApiOperation("更新角色")
    @PutMapping("/updateRole")
    public JsonResult updateRole(@RequestBody @Valid RoleResourceVo role) {
        SysRole roleById = sysRoleService.getById(role.getId());
        if (ObjectUtil.isNull(roleById)) {
            return result.error("角色不存在！");
        }
        if (ObjectUtil.isNotNull(role)) {
            int updateNum = sysRoleService.updateRoleLinkResource(role);
            if (updateNum>0) {
                result.success("修改角色成功");
            } else {
                result.error("修改角色失败！");
            }
        } else {
            result.error("请传入正确参数！");
        }
        return result;
    }

    @SysLogAnnotation("根据角色id删除角色")
    @ApiOperation("根据角色id删除角色")
    @Override
    public JsonResult removeById(@NotNull(message = "id不能为空") Long id) {
        SysRole sysRole = sysRoleService.getById(id);
        if (ObjectUtil.isNotNull(sysRole)) {
            boolean rsg = sysRoleService.removeRoleAndLinkedResourceById(id);
            if (rsg) {
                result.success("删除成功");
            } else {
                result.error("删除失败！");
            }
        } else {
            result.error("角色不存在！");
        }
        return result;
    }

}
